package com.xlhj.boot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: caijliu
 * @Date: 04/09/2021 12:19
 * @Description 延迟队列(队列TTL)配置类
 * @Vsersion: 0.0.1
 */
@Configuration
public class TtlQueueConfig {

    private static final String TTL_QUEUE_EXCHANGE = "ttl-queue-exchange";
    private static final String TTL_QUEUE_A = "ttl-queue-A";
    private static final String TTL_QUEUE_B = "ttl-queue-B";
    private static final String TTL_QUEUE_A_ROUT_KEY = "ttl_queue-A-routing-key";
    private static final String TTL_QUEUE_B_ROUT_KEY = "ttl_queue-B-routing-key";

    private static final String DEAD_LETTER_EXCHANGE = "dead-letter-exchange";
    private static final String DEAD_LETTER_QUEUE = "dead-letter-queue";
    private static final String DEAD_LETTER_ROUT_KEY = "dead-letter-routing-key";

    @Bean(name = "ttlQueueExchange")
    public DirectExchange ttlQueueExchange() {
        return new DirectExchange(TTL_QUEUE_EXCHANGE);
    }

    @Bean(name = "deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    @Bean(name = "ttlQueueA")
    public Queue ttlQueueA() {
        Map<String, Object> map = new HashMap<>(3);
        map.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", DEAD_LETTER_ROUT_KEY);
        map.put("x-message-ttl", 10000);
        return QueueBuilder.durable(TTL_QUEUE_A).withArguments(map).build();
    }

    @Bean
    public Binding ttlQueueBindingA(@Qualifier("ttlQueueA") Queue queue, @Qualifier("ttlQueueExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(TTL_QUEUE_A_ROUT_KEY);
    }

    @Bean(name = "ttlQueueB")
    public Queue ttlQueueB() {
        Map<String, Object> map = new HashMap<>(3);
        map.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", DEAD_LETTER_ROUT_KEY);
        map.put("x-message-ttl", 40000);
        return QueueBuilder.durable(TTL_QUEUE_B).withArguments(map).build();
    }

    @Bean
    public Binding ttlQueueBindingB(@Qualifier("ttlQueueB") Queue queue, @Qualifier("ttlQueueExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(TTL_QUEUE_B_ROUT_KEY);
    }

    @Bean(name = "deadLetterQueue")
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE);
    }

    @Bean
    public Binding deadLetterBinding(@Qualifier("deadLetterQueue") Queue queue, @Qualifier("deadLetterExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_ROUT_KEY);
    }
}
